<?php
abstract class DBMObject{
	
	/**
	 * Begins a transaction
	 * @param boolean $autocommit True if the autocommit should be setted as true, otherwise, false
	 * @param string $connectionID The connection identification (default = 'default')
	 * @return void
	 * @static
	 */
	public static function beginTransaction($autocommit = false, $connectionID = "default"){
		// get the connection
		$connection = DBMConnectionManager::getInstance()->getConnection($connectionID);
		
		// start the transaction
		$connection->beginTransaction($autocommit);
	}
	
	
	/**
	 * Commit an opened transaction
	 * @param string $connectionID The connection identification (default = 'default')
	 * @return void
	 * @static
	 */
	public static function commitTransaction($connectionID = "default"){
		// get the connection
		$connection = DBMConnectionManager::getInstance()->getConnection($connectionID);
		
		// start the transaction
		$connection->commitTransaction();
	}
	
	
	/**
	 * Rollback an opened transaction
	 * @param string $connectionID The connection identification (default = 'default')
	 * @return void
	 * @static
	 */
	public static function rollbackTransaction($connectionID = "default"){
		// get the connection
		$connection = DBMConnectionManager::getInstance()->getConnection($connectionID);
		
		// start the transaction
		$connection->rollbackTransaction();
	}
	
	
	/**
	 * Find an entity by it's primary key 
	 * @param string $entityName The entity name
	 * @param mixed $primaryKey The primary key value
	 * @param string $connectionID The connection identification (default = 'default')
	 * @return object The found entity
	 * @static
	 */
	public static function find($entityName, $primaryKey, $connectionID = "default"){
		// get an instance of the entity manager
		$entityMgr = DBMEntityManagerFactory::create($connectionID);
	
		// find
		return $entityMgr->find($entityName, $primaryKey);
	}
	
	
	/**
	 * Insert this object in the database
	 * @param string $connectionID The connection identification (default = 'default')
	 * @return mixed The identity of the inserted row in the database (primary key)
	 */
	public function insert($connectionID = "default"){
		// get an instance of the entity manager
		$entityMgr = DBMEntityManagerFactory::create($connectionID);
	
		// insert
		return $entityMgr->insert($this);
	}
	
	
	/**
	 * Update this object in the database
	 * @param string $connectionName The connection identification (default = 'default')
	 * @return int Number of affected rows
	 */
	public function update($connectionName = "default"){
		throw new Exception("Not implemented!");
	}
	
	/**
	 * Delete this object from the database
	 * @param string $connectionName The connection identification (default = 'default')
	 * @return int Number of affected rows
	 */
	public function delete($connectionName = "default"){
		throw new Exception("Not implemented!");
	}
}